package com.wbhz.mapper;

import java.util.HashMap;
import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.wbhz.entity.Collect;
import com.wbhz.entity.Like;
import com.wbhz.entity.Post;
import com.wbhz.entity.Reply;
import com.wbhz.entity.Tab;
import com.wbhz.entity.Vip;
@Mapper
public interface PostMapper {
	@Select("select * from t_post where post_id=#{postId}")
	Post selectPostDetail(@Param("postId")int postId);
	
//	@Insert("insert into t_post values(null, #{postUserId}, #{postTitle}, #{postContent}, #{postTabId}, #{postReward}, #{postStatus}, #{postStick}, #{postBrowse}, #{postReply}, #{postCollect}, #{postCreateTime}, #{postUpdateTime} )")
	@Insert("insert into t_post values(null, #{postUserId}, #{postTitle}, #{postContent}, #{postTabId}, #{postReward}, '未结贴', '未置顶', 0, 0, 0, now(), now() )")
	@Options(useGeneratedKeys=true)
	int insertPost(Post post);
	
	@Select("select * from t_tab")
	List<Tab> selectTab();
	// 分页显示？？标题模糊查询？？
	@Select("select * from t_post "
			+ "where (''=#{title} or post_title=#{title}) "
			+ "order by post_id desc "	// 逆序
			+ "limit #{start}, #{size}")
	List<Post> selectByPage(HashMap<String,Object> map);
	@Select("select count(*) from t_post")
	Integer selectCount();
	// t_tab
	@Select("select tab_id,tab_name,post_stick from t_tab,t_post where tab_id=post_tab_id order by post_id desc")
	List<Tab> selectTabByPost();
	// t_vip
	@Select("select * from t_vip where vip_userId=#{userId}")
	Vip selectVip(@Param("userId")int userId);
	// 置顶帖子与否
	@Update("update t_post set post_stick='已置顶' where post_id=#{postId}")
	int updateStickPostY(@Param("postId")int postId);
	@Update("update t_post set post_stick='未置顶' where post_id=#{postId}")
	int updateStickPostN(@Param("postId")int postId);
	// t_reply 评论帖子
	@Insert("insert into t_reply values(null, #{replyPostId}, #{replyPostUserId}, #{replyFromReplyUserId}, #{replyByReplyUserId}, 0, #{replyContent}, '普通回帖', now() )")
	@Options(useGeneratedKeys=true)
	int insertReply(Reply reply);
	
	@Select("select * from t_reply where reply_postId=#{replyPostId} order by reply_id desc")
	List<Reply> selectReplyByPage(HashMap<String,Object> map);
	
	@Update("update t_reply set reply_status='最佳答案' where reply_id=#{replyId}")
	int updateReplyStatus(@Param("replyId")int replyId);
	
	@Update("update t_post set post_status='已结贴' where post_id=#{postId}")
	int updatePostStatus(@Param("postId")int postId);
	
	@Update("update t_like set post_status='已结贴' where post_id=#{postId}")
	int updateReplyLike(@Param("postId")int postId);
	// 点赞信息表
	@Select("select * from t_like where like_postId=#{likePostId} "
			+ "and like_postUserId=#{likePostUserId} "
			+ "and like_replyId=#{likeReplyId} "
			+ "and like_fromLikeUserId=#{likeFromUserId} "
			+ "and like_byLikeUserId=#{likeByUserId}")
	Integer selectLike(@Param("likePostId")int likePostId, @Param("likePostUserId")int likePostUserId, @Param("likeReplyId")int likeReplyId, @Param("likeFromUserId")int likeFromUserId, @Param("likeByUserId")int likeByUserId);
	@Insert("insert into t_like values(null, #{likePostId}, #{likePostUserId}, #{likeReplyId}, #{likeFromUserId}, #{likeByUserId}, now())")
	@Options(useGeneratedKeys=true)
	int insertLike(@Param("likePostId")int likePostId, @Param("likePostUserId")int likePostUserId, @Param("likeReplyId")int likeReplyId, @Param("likeFromUserId")int likeFromUserId, @Param("likeByUserId")int likeByUserId);
	@Delete("delete from t_like where like_postId=#{likePostId} "
			+ "and like_postUserId=#{likePostUserId} "
			+ "and like_replyId=#{likeReplyId} "
			+ "and like_fromLikeUserId=#{likeFromUserId} "
			+ "and like_byLikeUserId=#{likeByUserId}")
	int deleteLike(@Param("likePostId")int likePostId, @Param("likePostUserId")int likePostUserId, @Param("likeReplyId")int likeReplyId, @Param("likeFromUserId")int likeFromUserId, @Param("likeByUserId")int likeByUserId);
	// 点赞数+-1
	@Update("update t_reply set reply_like=reply_like+1 where reply_id=#{replyId}")
	int addReplyLike(@Param("replyId")int replyId);
	@Update("update t_reply set reply_like=reply_like-1 where reply_id=#{replyId}")
	int subReplyLike(@Param("replyId")int replyId);
	
	@Insert("insert into t_report values(null, #{postId}, #{postUserId}, #{reportUserId}, #{postTitle}, '我要举报', '未审核', now() )")
	@Options(useGeneratedKeys=true)
	int insertReport(@Param("postId")int postId, @Param("postUserId")int postUserId, @Param("reportUserId")int reportUserId, @Param("postTitle")String postTitle);
	
	@Select("select * from t_report where report_postId=#{reportPostId}")
	Integer selectReport(@Param("reportPostId")int postId);
	
	@Select("select * from t_report where report_postId=#{reportPostId} and report_reportUserId=#{reportUserId}")
	Integer selectReportByUId(@Param("reportPostId")int postId, @Param("reportUserId")int reportUserId);
	// 收藏
	@Select("select * from t_collect where collect_postId=#{postId} "
			+ "and collect_userId=#{userId}")
	Integer selectCollect(@Param("postId")int postId, @Param("userId")int userId);
	@Insert("insert into t_collect values(null, #{postId}, #{postUserId}, #{userId}, now() )")
	@Options(useGeneratedKeys=true)
	int insertCollect(@Param("postId")int postId, @Param("postUserId")int postUserId, @Param("userId")int userId);
	@Delete("delete from t_collect where collect_postId=#{postId} "
			+ "and collect_postUserId=#{postUserId} "
			+ "and collect_userId=#{userId}")
	int deleteCollect(@Param("postId")int postId, @Param("postUserId")int postUserId, @Param("userId")int userId);
	
	@Insert("insert into t_message values(null, #{fromUserId}, #{byUserId}, #{msgName}, #{msgContent}, #{msgStatus}, now() )")
	@Options(useGeneratedKeys=true)
	int insertMsg(@Param("fromUserId")int fromUserId, @Param("byUserId")int byUserId, @Param("msgName")String msgName, @Param("msgContent")String msgContent, @Param("msgStatus")String msgStatus);
	// 浏览量+1
	@Update("update t_post set post_browse=post_browse+1 where post_id=#{postId}")
	int addPostBrowse(@Param("postId")int postId);
	// 浏览量+1，多余？
	@Update("update t_post set post_reply=post_reply+1 where post_id=#{postId}")
	int addPostReply(@Param("postId")int postId);
	// 通过postId，查找点赞表
	@Select("select * from t_like where like_postId=#{postId} and like_fromLikeUserId=#{userId}")
	List<Like> selectLikeByUserId(@Param("userId")int userId, @Param("postId")int postId);
	@Select("select * from t_reply where reply_postId=#{postId}")
	List<Reply> selectReplyByPId(@Param("postId")int postId);
	
	@Select("select * from t_post where post_user_id=#{userId} order by post_id desc")
	List<Post> selectPostByUId(@Param("userId")int userId);
	
	@Select("select * from t_collect where collect_userId=#{userId} order by collect_id desc")
	List<Collect> selectUserCollect(@Param("userId")int userId);
	
	@Select("select * from t_post,t_collect where collect_userId=#{userId} and collect_postId=post_id order by post_id desc")
	List<Post> selectUserCollectPost(@Param("userId")int userId);
	
	@Select("select user_name from t_user,t_post where post_user_id=user_id and post_id=#{postId}")
	String selectUserNameByPid(@Param("postId")int postId);
	// 最近热帖
	@Select("select * from t_post order by post_browse desc limit 0,10")
	List<Post> selectPostByBrowse();
	// 近期热议
	@Select("select * from t_post order by post_reply desc limit 0,10")
	List<Post> selectPostByReply();
	
	@Update("update t_user set user_money=user_money+#{postReward} where user_id=#{replyFromUserId}")
	int updateUserMoney(@Param("replyFromUserId")int replyFromUserId, @Param("postReward")int postReward);
	
	@Select("select post_user_id from t_post where post_id=#{postId}")
	int selectPostUserId(@Param("postId")int postId);
}
